package com.bianguo.android.bprinter.view.widget.popup;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.PopupWindow;

import com.bianguo.android.ioclibrary.ButterKnife;

import java.lang.reflect.Field;

public abstract class BasePopupWindow {

    protected Activity mContext;
    protected PopupWindow mPopupWindow;
    protected View mPopupView, mAnimView, mDisAnimView;
    protected Animation showAnim, disAnim;
    protected boolean isShowing;

    public BasePopupWindow(Activity mContext) {
        this(mContext, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    }

    public BasePopupWindow(Activity mContext, int width, int height) {
        initView(mContext, width, height);
    }

    protected void initView(Activity mContext, int width, int height) {
        this.mContext = mContext;
        mPopupView = getPopupView();
        ButterKnife.bind(this, mPopupView);
        mAnimView = getAnimView();
        mDisAnimView = getAnimView();
        showAnim = getShowAnim();
        disAnim = getDisAnim();

        mPopupWindow = new PopupWindow(mPopupView, width, height);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setFocusable(true);
        mPopupWindow.setAnimationStyle(0);
        mPopupWindow.setBackgroundDrawable(new ColorDrawable());
        fitPopupWindowOverStatusBar(true);
    }

    public void showPopupWindow() {
        try2ShowPopup(0, null);
    }

    public void showPopupWindow(int res) {
        try2ShowPopup(res, null);
    }

    public void showPopupWindow(View v) {
        try2ShowPopup(0, v);
    }

    private void try2ShowPopup(int res, View v) {
        //传递了view
        if (res == 0 && v != null) {
            int[] location = new int[2];
            v.getLocationOnScreen(location);
            mPopupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0] - 8, location[1] + v.getHeight());
        }
        //传递了res
        if (res != 0 && v == null) {
            mPopupWindow.showAtLocation(mContext.findViewById(res), Gravity.CENTER, 0, 0);
        }
        //什么都没传递，取顶级view的id
        if (res == 0 && v == null) {
            mPopupWindow.showAtLocation(mContext.findViewById(android.R.id.content), Gravity.CENTER, 0, 0);
        }
        if (showAnim != null && mAnimView != null) {
            mAnimView.clearAnimation();
            mAnimView.startAnimation(showAnim);
        }
        isShowing = true;
    }

    public void dismiss() {
        if (disAnim != null && mAnimView != null) {
            disAnim.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    mPopupWindow.dismiss();
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
            mAnimView.clearAnimation();
            mAnimView.startAnimation(disAnim);
        } else {
            mPopupWindow.dismiss();
        }
        isShowing = false;
    }

    public void fitPopupWindowOverStatusBar(boolean needFullScreen) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            try {
                Field mLayoutInScreen = PopupWindow.class.getDeclaredField("mLayoutInScreen");
                mLayoutInScreen.setAccessible(true);
                mLayoutInScreen.set(mPopupWindow, needFullScreen);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    protected void showView(View view) {
        if (View.VISIBLE != view.getVisibility()) {
            view.setVisibility(View.VISIBLE);
        }
    }

    protected void hideView(View view) {
        hideView(view, View.GONE);
    }

    protected void hideView(View view, int visibility) {
        if (View.VISIBLE == view.getVisibility()) {
            view.setVisibility(visibility);
        }
    }

    public abstract View getPopupView();

    public abstract View getAnimView();

    public abstract Animation getShowAnim();

    public abstract Animation getDisAnim();
}
